﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace App
{

    using ECommon.Components;
    using ECommon.Utilities;
    using ENode.Commanding;
    using ENode.Configurations;
    using ENode.EQueue;
    using ENode.EQueue.Commanding;
    using ENode.Infrastructure;
    using ENode.Infrastructure.Impl;
    using EQueue.Clients.Producers;
    using EQueue.Configurations;
    using Infrastructure;

    using Commands.Accounts;
    using Domain.Accounts;
    using Messages;
    using ProcessManagers;
    using Denormalizers.Ef;
    using ECommon.Socketing;

    public static class ENodeExtensions
    {

        private static CommandService _commandService;

        public static ENodeConfiguration RegisterAllTypeCodes(this ENodeConfiguration enodeConfiguration)
        {
            var provider = ObjectContainer.Resolve<ITypeCodeProvider>() as DefaultTypeCodeProvider;


            //aggregates
            provider.RegisterType<Account>(AllTypeCodes.Account);

            //commands
            provider.RegisterType<RegisterNewAccountCommand>(AllTypeCodes.RegisterNewAccountCommand);
            //provider.RegisterType<AcceptNewSignInCommand>(AllTypeCodes.AcceptNewSignInCommand);

            //events
            provider.RegisterType<NewAccountRegisteredEvent>(AllTypeCodes.NewAccountRegisteredEvent);

            //application message
            //provider.RegisterType<SignInRejectedMessage>(AllTypeCodes.SignInRejectedMessage);

            //publishable exceptions
            //provider.RegisterType<DisabledException>(AllTypeCodes.DisabledException);

            //application message, domain event, or exception handlers
            //provider.RegisterType<OAuthProcessManager>(AllTypeCodes.OAuthProcessManager);
            provider.RegisterType<AccountDenormalizer>(AllTypeCodes.AccountDenormalizer);

            return enodeConfiguration;
        }
        public static ENodeConfiguration UseEQueue(this ENodeConfiguration enodeConfiguration)
        {
            var configuration = enodeConfiguration.GetCommonConfiguration();

            configuration.RegisterEQueueComponents();

            _commandService = new CommandService(
                new CommandResultProcessor(new IPEndPoint(SocketUtils.GetLocalIPV4(), 9001)),//使用这个去连接eq？
                "CommandService",
                new ProducerSetting { BrokerAddress = new IPEndPoint(SocketUtils.GetLocalIPV4(), ConfigSettings.BrokerProducerPort) });

            configuration.SetDefault<ICommandService, CommandService>(_commandService);

            return enodeConfiguration;
        }

        public static ENodeConfiguration Start(this ENodeConfiguration enodeConfiguration)
        {
            _commandService.Start();
            return enodeConfiguration;
        }

    }
}
